package cn.hy.preparelesson.common.service;

import cn.hy.preparelesson.common.entity.PlLessonFile;
import cn.hy.preparelesson.common.entity.PlLessonFileExample;
import cn.hy.preparelesson.common.mapper.PlLessonFileMapper;
import cn.hy.preparelesson.common.utils.IdUtils;
import cn.hy.preparelesson.common.vo.FilePlLessonVo;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 备课 - 附件 Service
 */
@Service
public class PlLessonFileService  extends  BaseService{
    @Autowired
    PlLessonFileMapper plLessonFileMapper;

    /**
     * 批量保存备课附件信息
     * @param plLessonFileList
     */
    @Transactional(rollbackFor = Exception.class)
    public void batchSavePlLessonFile(List<PlLessonFile> plLessonFileList){
        if (!CollectionUtils.isEmpty(plLessonFileList) ) {
            plLessonFileMapper.batchSavePlLessonFile(plLessonFileList);
        }
    }

    /**
     * 备课 - 文件 list
     * @param schoolId
     * @param lessonId
     * @return
     */
    public List<PlLessonFile> findPlLessonFileListBySchoolIdAndLessonId(String schoolId, String lessonId) {
        PlLessonFileExample example = new PlLessonFileExample();
        example.createCriteria().andSchoolIdEqualTo(schoolId).andLessonIdEqualTo(lessonId);
        example.setOrderByClause("sort ASC");
        return  plLessonFileMapper.selectByExample(example);
    }

    /**
     * 批量修改备课-附件
     * @param schoolId
     * @param opeUserId
     * @param lessonId
     * @param annexListStr
     * @param now
     */
    @Transactional(rollbackFor = Exception.class)
    public void batchUpdatePlLessonFile(String schoolId, String opeUserId, String lessonId, String annexListStr, Date now) {
        batchDeletePlLessonFile(schoolId, opeUserId, lessonId);
        if (StringUtils.isNotBlank(annexListStr)) {
            List<FilePlLessonVo> filePlLessonVoList = JSONObject.parseObject(annexListStr, new TypeReference<List<FilePlLessonVo>>(){});
            List<PlLessonFile> plLessonFileList = new ArrayList<>();
            filePlLessonVoList.stream().forEach(anndex -> plLessonFileList.add(new PlLessonFile(IdUtils.newId(), anndex.getFileName(), anndex.getFilePath(), anndex.getSort(), lessonId, schoolId, opeUserId, now )));
            batchSavePlLessonFile( plLessonFileList);
        }
    }

    /**
     * 批量删除备课 - 附件
     * @param schoolId
     * @param opeUserId
     * @param lessonId
     */
    @Transactional(rollbackFor = Exception.class)
    public void  batchDeletePlLessonFile(String schoolId, String opeUserId, String lessonId) {
        PlLessonFileExample example = new PlLessonFileExample();
        example.createCriteria().andSchoolIdEqualTo(schoolId).andCreateByEqualTo(opeUserId).andLessonIdEqualTo(lessonId);
        plLessonFileMapper.deleteByExample(example);
    }

}
